/**
 * 随机生成字符串
 * @param len
 * @returns {string}
 */
function genRandomString(len) {
  len = len || 32;
  var $chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678'; // 默认去掉了容易混淆的字符oOLl,9gq,Vv,Uu,I1
  var maxPos = $chars.length;
  var pwd = '';
  for (let i = 0; i < len; i++) {
    pwd += $chars.charAt(Math.floor(Math.random() * maxPos));
  }
  return pwd;
}

/**
 * 字符串非空判断
 * @param str
 * @returns
 */
function isEmpty(str) {
    if (str == null) {
        return true;
    }
    if (str == undefined) {
        return true;
    }
    if (str.trim() == "") {
        return true;
    }
    return false;
}

function formatDtm(dtm) {
    if(isEmpty(dtm)){
        return "";
    }else{
        var year = dtm.substr(0, 4);
        var month = dtm.substr(4, 2);
        var day = dtm.substr(6, 2);
        var hour = dtm.substr(8, 2);
        var min = dtm.substr(10, 2);
        var sec = dtm.substr(12, 2);
        return year + "-" + month + "-" + day + " " + hour + ":" + min + ":" + sec+"";
    }
}

function formatDt(dt) {
    if(isEmpty(dt)){
        return "";
    }else{
        dt = dt+"";
        // console.info(dt);
        var year = dt.substr(0, 4);
        var month = dt.substr(4, 2);
        var day = dt.substr(6, 2);
        return year + "年" + month + "月" + day+"日";
    }
}

function replaceAll(str, sptr, sptr1) {
    while (str.indexOf(sptr) >= 0) {
        str = str.replace(sptr, sptr1);
    }
    return str;
}

function formatYDMHMSDtm(dtm) {
    if(isEmpty(dtm)){
        return "";
    }else{
        dtm = this.replaceAll(dtm, "-", "");
        dtm = this.replaceAll(dtm, ":", "");
        dtm = this.replaceAll(dtm, " ", "");
        return dtm;
    }
}

export default {
  genRandomString,
  isEmpty,
  formatDtm,
  formatDt,
  formatYDMHMSDtm,
  replaceAll
};